package com.xiaomaoguai.activiti;

import java.io.File;
import java.io.InputStream;
import java.util.List;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.springframework.util.FileCopyUtils;

public class HelloWorldTest {

	private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

	/**
	 * 测试生成--25张表
	 */
	@Test
	public void testCreateTable() {
		System.out.println(processEngine.getName());
	}

	/**
	 * 部署流程定义
	 */
	@Test
	public void testDeploy() {
		RepositoryService repositoryService = processEngine.getRepositoryService();
		Deployment deployment = repositoryService.createDeployment().name("helloworld入门程序") // 创建一个部署对象
				.addClasspathResource("diagrams/helloworld.bpmn").addClasspathResource("diagrams/helloworld.png")
				.deploy();
		System.out.println("部署ID：" + deployment.getId());// 1
		System.out.println("部署名称：" + deployment.getName());// helloworld入门程序
	}

	/**
	 * 部署流程定义
	 */
	@Test
	public void testDeploy2() {
		RepositoryService repositoryService = processEngine.getRepositoryService();
		Deployment deployment = repositoryService.createDeployment().name("helloworldProcess") // 创建一个部署对象
				.addClasspathResource("diagrams/process.bpmn20.xml")
				.deploy();
		System.out.println("部署ID：" + deployment.getId());// 1
		System.out.println("部署名称：" + deployment.getName());// helloworld入门程序
	}

	/**
	 * 部署流程定义
	 */
	@Test
	public void getDeployPic() throws Exception {
		RepositoryService repositoryService = processEngine.getRepositoryService();
		List<String> resourceNames = repositoryService.getDeploymentResourceNames("27501");
		String pic = null;
		for (String resourceName : resourceNames) {
			System.out.println(resourceName);
			if (resourceName.endsWith(".png")) {
				pic = resourceName;
				break;
			}
		}
		InputStream stream = repositoryService.getResourceAsStream("27501", pic);
		File file = new File("f:/2.png");
		byte[] by = FileCopyUtils.copyToByteArray(stream);
		FileCopyUtils.copy(by, file);
	}

	/**
	 * 启动流程实例
	 */
	@Test
	public void startProcessInstance() {
		// 流程定义的key
		String processDefinitionKey = "process";
		ProcessInstance pi = processEngine.getRuntimeService()// 与正在执行的流程实例和执行对象相关的Service
				.startProcessInstanceByKey(processDefinitionKey);// 使用流程定义的key启动流程实例，key对应helloworld.bpmn文件中id的属性值，使用key值启动，默认是按照最新版本的流程定义启动
		System.out.println("流程实例ID:" + pi.getId());// 流程实例ID 101
		System.out.println("流程定义ID:" + pi.getProcessDefinitionId());// 流程定义ID
		// helloworld:1:4

		//act_ru_execution 这个表
	}

	@Test
	public void findMyPersonalTask() {
		String assignee = "张三";
		// 与正在执行的任务管理相关的Service
		TaskService taskService = processEngine.getTaskService();

		List<Task> list = taskService.createTaskQuery(). // 创建任务查询对象
				taskAssignee(assignee)// 指定个人任务查询，指定办理人
				.list();
		if (list != null && !list.isEmpty()) {
			for (Task task : list) {
				System.out.println("任务ID:" + task.getId());
				System.out.println("任务名称:" + task.getName());
				System.out.println("任务的创建时间:" + task.getCreateTime());
				System.out.println("任务的办理人:" + task.getAssignee());
				System.out.println("流程实例ID：" + task.getProcessInstanceId());
				System.out.println("执行对象ID:" + task.getExecutionId());
				System.out.println("流程定义ID:" + task.getProcessDefinitionId());
				System.out.println("########################################################");
			}
		}
	}

	/**
	 * 完成我的任务
	 */
	@Test
	public void completeMyPersonalTask() {
		//任务ID
		String taskId = "2504";
		processEngine.getTaskService()//与正在执行的任务管理相关的Service
				.complete(taskId);
		System.out.println("完成任务：任务ID：" + taskId);
	}

}
